package com.develop.controller;



import com.develop.common.utils.R;
import com.develop.common.utils.cloud.OSSFactory;
import com.develop.entity.SysOssEntity;
import com.develop.service.SysOssService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Api(value = "上传接口", tags = {"上传接口"})
@RestController
@RequestMapping("/api/upload")
public class UploadController  {
@Autowired
private SysOssService sysOssService;
    @ApiOperation(value = "上传文件、图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "TokenID", value = "用户登录令牌", paramType = "query", required = true, dataType = "String"),

    })
    @PostMapping(value = "/upload" ,headers="content-type=multipart/form-data")
    public R upload(@ApiParam(name = "file",value = "file", required = true) MultipartFile file) throws Exception {


        if (file.isEmpty()) {


            return R.error("请上传文件");
        }
        long size = file.getSize();



//上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

        //保存文件信息
        SysOssEntity ossEntity = new SysOssEntity();
        ossEntity.setUrl(url);
        ossEntity.setCreateDate(new Date());
        sysOssService.save(ossEntity);

        return R.ok(url);
    }





}
